package telsoft.cms_admin.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * @author Shirin Joshi 
 * Here operator: 
 *   gt for Greater Than 
 *   lt for Less Than
 *      eq for EqualTo 
 *      gq for Greater and Equal To 
 *      lq for Less and Equal To 
 *      nt for Not Equal To
 * 
 */
public class DateCompareValidator implements Validator {

    private String operator = "";
    private Date compareWith = new Date();
    private String compareFieldName = "";

    /*
     * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext,
     *      javax.faces.component.UIComponent, java.lang.Object)
     */
    public void validate(FacesContext context, UIComponent component, Object value)
            throws ValidatorException {

        boolean isVailid = true;

//        System.out.println("=============");
//        System.out.println("value :: " + value);

        initProps(component, context);

//        System.out.println("operator :: " + operator);
//        System.out.println("compareWith :: " + compareWith);
//        System.out.println("compareFieldName :: " + compareFieldName);
//        System.out.println("=============");

        FacesMessage message = new FacesMessage();
        Date thisDate = null;

        try {
            thisDate = (Date) value;
        } catch (Exception e) {
//            System.out.println("Error in validate method of DateCompareValidator : " + e);
        }

        if (thisDate == null) {
            thisDate = new Date();
        }

        int result = thisDate.compareTo(compareWith);

        String compareData = "";

        if (compareFieldName.trim().length() != 0) {
            compareData = compareFieldName;
        } else {
            compareData = new SimpleDateFormat("MM/dd/yyyy").format(compareWith);

        }

        /*
         * required >= and we found the result less than
         */
        String thisName = (String) component.getAttributes().get("thisName");
        String compareDes = (String) component.getAttributes().get("compareDes");
        if (result < 0 && operator.equalsIgnoreCase("ge")) {
            thisName = subChar(thisName, ":");
            compareData = subChar(compareData, "(*):");
            message.setDetail(thisName + " " + compareDes + " " + compareData);
            message.setSummary(thisName + " " + compareDes + " " + compareData);
            isVailid = false;
        } else /*
         * required <= and we found the result greater than
         */ if (result > 0 && operator.equalsIgnoreCase("le")) {
            message.setDetail("This date should <= " + compareData);
            message.setSummary("This date should <= " + compareData);
            isVailid = false;
        } else /*
         * required == and we found the result not equal
         */ if (result != 0 && operator.equalsIgnoreCase("eq")) {
            message.setDetail("This date should == " + compareData);
            message.setSummary("This date should == " + compareData);
            isVailid = false;
        } else /*
         * required > and we found the result less than equal too
         */ if (result <= 0 && operator.equalsIgnoreCase("gt")) {
            message.setDetail("This date should > " + compareData);
            message.setSummary("This date should > " + compareData);
            isVailid = false;
        } else /*
         * required <>
         */ if (result >= 0 && operator.equalsIgnoreCase("lt")) {
            message.setDetail("This date should < " + compareData);
            message.setSummary("This date should < " + compareData);
            isVailid = false;
        } else /*
         * required != and we found the result equal too
         */ if (result == 0 && operator.equalsIgnoreCase("nt")) {
            message.setDetail("This date should not = " + compareData);
            message.setSummary("This date should not = " + compareData);
            isVailid = false;
        }


        if (!isVailid) {
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

    /*
     * This method will set the value of both operator and date to compare.
     */
    private void initProps(UIComponent component, FacesContext context) {

        Object compareWith = component.getAttributes().get("compareWith");

        HtmlInputText ht = (HtmlInputText) context.getViewRoot().findComponent(compareWith.toString());

        if (ht != null) {
            compareWith = ht.getValue();
        }

//        System.out.println("compareWith -----------::: " + compareWith);
        operator = (String) component.getAttributes().get("operator");
        if (operator == null) {
            operator = ">";
        }

        compareFieldName = (String) component.getAttributes().get("compareFieldName");
        if (compareWith != null) {
            try {
                this.compareWith = (Date) compareWith;
            } catch (Exception e) {
//                System.out.println("Error in parsing the Date : " + e);
            }
        }

    }

    public String subChar(String str1, String char1) {
        int iIndex = str1.lastIndexOf(char1);
        if (iIndex > 0) {
            str1 = str1.substring(0, iIndex);
        }
        return str1;
    }
}
